package Stream;

import java.io.*;

public class DirectoryTest {

    public static void main(String[] args) {
        String dirname = "JavaLearnSample\\src\\Stream\\NewFolder";
        File d = new File(dirname);
        // 现在创建目录
        if (!d.isDirectory()) {
            d.mkdirs();
        } else {
            System.out.println("目录已经存在：" + dirname);
        }

        /**
         * 遍历目录
         */
        dirname = "JavaLearnSample\\src\\Stream";
        File f1 = new File(dirname);
        if (f1.isDirectory()) {
            System.out.println("目录 " + dirname);
            String s[] = f1.list();
            for (int i = 0; i < s.length; i++) {
                File f = new File(dirname + "/" + s[i]);
                if (f.isDirectory()) {
                    System.out.println(s[i] + " 是一个目录");
                } else {
                    System.out.println(s[i] + " 是一个文件");
                }
            }
        } else {
            System.out.println(dirname + " 不是一个目录");
        }

        // 删除目录及文件
        File folder = new File(dirname + "\\NewFolder");
        deleteFolder(folder);
    }

    // 删除文件及目录
    public static void deleteFolder(File folder) {
        File[] files = folder.listFiles();
        if (files != null) {
            for (File f : files) {
                if (f.isDirectory()) {
                    deleteFolder(f);
                } else {
                    f.delete();
                }
            }
        }
        folder.delete();
    }
}
